package com.wulin;

import com.wulin.mapper.EnvironmentDataMapper;
import com.wulin.mapper.UserDataMapper;
import com.wulin.pojo.EnvironmentData;
import com.wulin.pojo.UserData;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import sun.font.CompositeFont;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;
import java.io.IOException;
import java.io.InputStream;

public class details_one extends JFrame {

	private JPanel contentPane;
	JTextField textField, textField_1;
	JTextField textField_3;
	private JLabel lblNewLabel_4;
	JTextField textField_5;
	private JLabel lblNewLabel_5;
	JTextField textField_6;
	private JLabel lblNewLabel_6;
	JTextField textField_4;
	JTextField textField_7;
	private JButton btnNewButton;

	String resource = "mybatis-config.xml";
	InputStream inputStream = Resources.getResourceAsStream(resource);
	SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);

	SqlSession sqlSession =sqlSessionFactory.openSession();


	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					details_one frame = new details_one();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public details_one() throws IOException {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1402, 823);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblNewLabel_2 = new JLabel("UID:");
		lblNewLabel_2.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_2.setBounds(233, 8, 457, 75);
		contentPane.add(lblNewLabel_2);
		
		textField = new JTextField();
		textField.setFont(new Font("宋体", Font.PLAIN, 26));
		textField.setBounds(690, 8, 414, 75);
		contentPane.add(textField);
		textField.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("collection_time:");
		lblNewLabel.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel.setBounds(233, 83, 457, 75);
		contentPane.add(lblNewLabel);
		
		textField_1 = new JTextField();
		textField_1.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_1.setBounds(690, 83, 414, 75);
		contentPane.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel_3 = new JLabel("temperature:");
		lblNewLabel_3.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_3.setBounds(233, 158, 457, 75);
		contentPane.add(lblNewLabel_3);
		
		textField_3 = new JTextField();
		textField_3.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_3.setBounds(690, 158, 414, 75);
		contentPane.add(textField_3);
		textField_3.setColumns(10);
		
		JLabel lblNewLabel_1 = new JLabel("humidity:");
		lblNewLabel_1.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_1.setBounds(233, 233, 457, 75);
		contentPane.add(lblNewLabel_1);
		
		textField_6 = new JTextField();
		textField_6.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_6.setBounds(690, 233, 414, 75);
		contentPane.add(textField_6);
		textField_6.setColumns(10);
		
		lblNewLabel_5 = new JLabel("light_intensity:");
		lblNewLabel_5.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_5.setBounds(233, 308, 457, 75);
		contentPane.add(lblNewLabel_5);
		
		textField_5 = new JTextField();
		textField_5.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_5.setBounds(690, 308, 414, 75);
		contentPane.add(textField_5);
		textField_5.setColumns(10);
		
		lblNewLabel_4 = new JLabel("soil_moisture:");
		lblNewLabel_4.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_4.setBounds(233, 383, 457, 75);
		contentPane.add(lblNewLabel_4);
		
		textField_4 = new JTextField();
		textField_4.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_4.setBounds(690, 383, 414, 75);
		contentPane.add(textField_4);
		textField_4.setColumns(10);
		
		lblNewLabel_6 = new JLabel("soil_fertility:");
		lblNewLabel_6.setFont(new Font("宋体", Font.PLAIN, 30));
		lblNewLabel_6.setBounds(233, 458, 457, 75);
		contentPane.add(lblNewLabel_6);
		
		textField_7 = new JTextField();
		textField_7.setFont(new Font("宋体", Font.PLAIN, 26));
		textField_7.setBounds(690, 458, 414, 75);
		contentPane.add(textField_7);
		textField_7.setColumns(10);
		
		btnNewButton = new JButton("删除");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int id = Integer.parseInt(textField.getText());
					EnvironmentDataMapper mapper = sqlSession.getMapper(EnvironmentDataMapper.class);
					int rows = mapper.delete(id);
					if (rows > 0) {
						JOptionPane.showMessageDialog(details_one.this, "删除成功", "成功", JOptionPane.CLOSED_OPTION);
					} else {
						JOptionPane.showMessageDialog(details_one.this, "删除失败", "失败", JOptionPane.ERROR_MESSAGE);
					}
					sqlSession.commit();
				} catch (Exception ex) {
					ex.printStackTrace();
					JOptionPane.showMessageDialog(details_one.this, "操作失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
					sqlSession.rollback();
				}
			}
//			public void actionPerformed(ActionEvent e) {
//				Integer id =Integer.valueOf(textField.getText());
//				String sql = "DELETE FROM environment_data WHERE UID = " + id + " ";
//				if(db.executeUpdate(sql)){
//					JOptionPane.showMessageDialog(details_one.this, "刪除成功","成功", JOptionPane.CLOSED_OPTION);
//				}
//				else{
//					JOptionPane.showMessageDialog(details_one.this, "刪除失败","失败", JOptionPane.ERROR_MESSAGE);
//				}
//				setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//			}
		});
		btnNewButton.setBounds(233, 582, 305, 75);
		contentPane.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("确认更改");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
					int id = Integer.parseInt(textField.getText());
					String collection_time = textField_1.getText();
					String temperature = textField_3.getText();
					String humidity = textField_6.getText();
					String lightIntensity = textField_5.getText();
					String soilMoisture = textField_4.getText();
					String soilFertility = textField_7.getText();

					// 创建 EnvironmentData 对象
					EnvironmentData environmentData = new EnvironmentData();
					environmentData.setUID(id);
					environmentData.setCollection_time(collection_time);
					environmentData.setTemperature(temperature);
					environmentData.setHumidity(humidity);
					environmentData.setLight_intensity(lightIntensity);
					environmentData.setSoil_moisture(soilMoisture);
					environmentData.setSoil_fertility(soilFertility);

					// 获取 Mapper 接口
					EnvironmentDataMapper mapper = sqlSession.getMapper(EnvironmentDataMapper.class);
					int rows = mapper.update(environmentData);
					if (rows > 0) {
						JOptionPane.showMessageDialog(details_one.this, "修改成功", "成功", JOptionPane.CLOSED_OPTION);
					} else {
						JOptionPane.showMessageDialog(details_one.this, "修改失败", "失败", JOptionPane.ERROR_MESSAGE);
					}
					sqlSession.commit(); // 提交事务
				} catch (Exception ex) {
					ex.printStackTrace();
					JOptionPane.showMessageDialog(details_one.this, "操作失败: " + ex.getMessage(), "错误", JOptionPane.ERROR_MESSAGE);
				}
//				Integer id =Integer.valueOf(textField.getText());
//				String cnte = textField_1.getText();
//				String tem = textField_3.getText();
//				String hum = textField_6.getText();
//				String light = textField_5.getText();
//				String slme = textField_4.getText();
//				String slfy = textField_7.getText();
//				String sql = "UPDATE environment_data SET collection_time = '"+ cnte +"' ,"
//						+ " temperature = "+ tem +" ,"
//							+ " humidity = "+ hum +" ,"
//								+ " light_intensity = "+ light + " ,"
//									+ " soil_moisture = "+ slme + " ,"
//										+ " soil_fertility = "+ slfy +""
//											+ " WHERE UID = " + id + "";
//				if(db.executeUpdate(sql)){
//					JOptionPane.showMessageDialog(details_one.this, "修改成功","成功", JOptionPane.CLOSED_OPTION);
//					getContentPane().setLayout(null);
//				}
//				else{
//					JOptionPane.showMessageDialog(details_one.this, "修改失败","失败", JOptionPane.ERROR_MESSAGE);
//				}
			}
		});
		btnNewButton_1.setBounds(799, 582, 305, 75);
		contentPane.add(btnNewButton_1);
	}
}
